import json

import redis
import logging
from logging import debug, error, info
import os

FORMAT = '  [%(levelname)-5s]%(asctime)-8s %(filename)s:%(lineno)d %(message)s'
DATEFORMAT = '%H:%M:%S'
logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATEFORMAT)

if __name__ == '__main__':
    import redis

    pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
    r = redis.Redis(connection_pool=pool)
    # run_worker(r)

    dbroot = '/home/d/disk/原始/'
    files = os.listdir(dbroot)

    outfiles = os.listdir('out_dir')
    for f in outfiles:
        os.remove('out_dir/' + f)
    RUN_NUM = len(files)
    # RUN_NUM = 8
    counter = RUN_NUM
    for file_name in files:
        counter -= 1
        debug(file_name)
        task = {
            'file_name' : dbroot + file_name,
            'dist' : 'test_result'
        }
        r.rpush('diagnose_tasks', json.dumps(task))
        if counter == 0:
            break

    for i in range(RUN_NUM):
        x = r.blpop('test_result')
        info(x[1].decode())
